package com.tico.editor.editors.postman;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@Service
public class HttpClientPostMan {

    public JSONObject doPost(String url, String param, Map<String,String> map) throws InvalidKeyException, NoSuchAlgorithmException {
        JSONObject json  = null;
        HttpClient httpClient;
        HttpPost postMethod;
        HttpResponse response;
        String reponseContent = null;
        try {
            httpClient = HttpClients.createDefault();
            postMethod = new HttpPost(url);
            for(String key:map.keySet()){
                postMethod.addHeader(key, map.get(key));
            }
            postMethod.addHeader("Content-Type", "application/json");
            postMethod.setEntity(new StringEntity(param, Charset.forName("UTF-8")));
            response = httpClient.execute(postMethod);
            HttpEntity httpEntity = response.getEntity();
            reponseContent = EntityUtils.toString(httpEntity);
            System.out.println("=POST====返回值======" + reponseContent);
            json= JSONObject.parseObject(reponseContent);
            EntityUtils.consume(httpEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    public JSONObject doGet(String url, String param, Map<String,String> map) throws InvalidKeyException, NoSuchAlgorithmException {
        JSONObject json  = null;
        HttpClient httpClient;
        HttpGet getMethod;
        HttpResponse response;
        String reponseContent = null;
        try {
            httpClient = HttpClients.createDefault();
            getMethod = new HttpGet(url);
            getMethod.addHeader("Content-type", "application/json; charset=UTF-8");
            for(String key:map.keySet()){
                getMethod.addHeader(key, map.get(key));
            }
            response = httpClient.execute(getMethod);
            HttpEntity httpEntity = response.getEntity();
            reponseContent = EntityUtils.toString(httpEntity);
            System.out.println("=GET====返回值======" + reponseContent);
            json = JSON.parseObject(reponseContent);
            EntityUtils.consume(httpEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    public JSONObject doPut(String url, String param, Map<String,String> map) throws InvalidKeyException, NoSuchAlgorithmException {
        JSONObject json  = null;
        HttpClient httpClient;
        HttpGet getMethod;
        HttpResponse response;
        String reponseContent = null;
        try {
            httpClient = HttpClients.createDefault();
            getMethod = new HttpGet(url);
            getMethod.addHeader("Content-type", "application/json; charset=UTF-8");
            for(String key:map.keySet()){
                getMethod.addHeader(key, map.get(key));
            }
            response = httpClient.execute(getMethod);
            HttpEntity httpEntity = response.getEntity();
            reponseContent = EntityUtils.toString(httpEntity);
            System.out.println("=GET====返回值======" + reponseContent);
            json = JSON.parseObject(reponseContent);
            EntityUtils.consume(httpEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    public JSONObject doDelete(String url, String param, Map<String,String> map) throws InvalidKeyException, NoSuchAlgorithmException {
        JSONObject jsonObject = null;
        HttpClient httpClient;
        HttpDelete httpdelete;
        HttpResponse response;
        String reponseContent = null;
        try {
            httpClient = HttpClients.createDefault();
            JSONObject parse = JSONObject.parseObject(param);
            HashMap<String,String> maps = new HashMap<String, String>();
            for(String keys:parse.keySet()){
                maps.put(keys,parse.getString(keys));
            }
            List<NameValuePair> formparams = setHttpParams(maps);
            String endParam = URLEncodedUtils.format(formparams, "UTF-8");
            httpdelete = new HttpDelete(URI.create(url + "?" + endParam));
            for(String key:map.keySet()){
                httpdelete.addHeader(key, map.get(key));
            }
            response = httpClient.execute(httpdelete);
            HttpEntity httpEntity = response.getEntity();
            reponseContent = EntityUtils.toString(httpEntity);
            System.out.println("=DELETE====返回值======" + reponseContent);
            jsonObject = JSON.parseObject(reponseContent);
            EntityUtils.consume(httpEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
    /**
     * 设置请求参数
     * @param map
     * @return
     */
    private static List<NameValuePair> setHttpParams(Map<String, String> map) {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        Set<Map.Entry<String, String>> set = map.entrySet();
        for (Map.Entry<String, String> entry : set) {
            formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        return formparams;
    }
}
